package com.roubsite.cloud.menu.service.impl;

import com.roubsite.cloud.menu.bean.RsMenu;
import com.roubsite.cloud.menu.bean.MenuMeta;
import com.roubsite.cloud.menu.bean.MenuRouteBean;
import com.roubsite.cloud.menu.dao.SystemMenuDao;
import com.roubsite.cloud.menu.service.ISystemMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SystemMenuService implements ISystemMenuService {
	@Autowired
	SystemMenuDao systemMenuDao;

	@Override
	public List<MenuRouteBean> getUserMenu(String parentId, String userId) {
		return getMenuByParentId(parentId, userId);
	}

	private List<MenuRouteBean> getMenuByParentId(String parentId, String userId) {
		List<RsMenu> rootList = systemMenuDao.getUserMenuByParentId(parentId, userId);
		List<MenuRouteBean> menuRouteBeans = new ArrayList<>();

		for (RsMenu menu : rootList) {
			MenuRouteBean menuRouteBean = new MenuRouteBean();
			menuRouteBean.setPath(StringUtils.isEmpty(menu.getMenuUrl())? "/"+menu.getMenuId() : menu.getMenuUrl());
			menuRouteBean.setName(menu.getMenuId());
			menuRouteBean.setComponent(StringUtils.isEmpty(menu.getComponent())? menu.getMenuId() : menu.getComponent());
			MenuMeta menuMeta = new MenuMeta();
			menuMeta.setTitle(menu.getMenuName());
			menuMeta.setIcon(menu.getMenuIcon());
			menuMeta.setHide(menu.getVisible() != 1);

			menuRouteBean.setMeta(menuMeta);
			menuRouteBean.setChildren(getMenuByParentId(menu.getMenuId(), userId));
			menuRouteBeans.add(menuRouteBean);
		}
		return menuRouteBeans;
	}
}
